<!DOCTYPE HTML>
<html>
<head>
<title>区块 | AutoHotkey</title>
<meta name="description" content="Blocks are one or more code lines enclosed in braces. Typically used with function definitions and control flow statements." />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>{...} (区块)</h1>

<p>一对大括号组成的区域表示一个区块. 区块通常与<a href="Functions.htm">函数</a>, <a href="Else.htm">Else</a>, <a href="Loop.htm">Loop</a>, <a href="While.htm">While 循环</a>和 IF 命令一起使用.</p>

<pre class="Syntax">
{
零行或多行命令
}</pre>
<h2>备注</h2>
<p>使用区块可以绑定两行或多行命令. 还可以用它改变 IF 与 <a href="Else.htm">ELSE</a> 从属关系, 例如在此例中, 区块强制 <a href="Else.htm">ELSE</a> 从属于首个 IF 而不是第二个:</p>
<pre>if var1 = 1
{
    if var2 = abc
        sleep, 1
}
else
    return</pre>
<p>尽管区块可以用在脚本中的任何位置, 不过目前它们仅在与<a href="Functions.htm">函数</a>, <a href="Else.htm">Else</a>, <a href="Loop.htm">Loop</a> 或 IF 类型的命令例如 <a href="IfEqual.htm">IfEqual</a> 或 <a href="WinExist.htm">IfWinExist</a> 一起使用时才有意义.</p>
<p>如果 <a href="IfEqual.htm">IF</a>, <a href="Else.htm">ELSE</a>, <a href="Loop.htm">Loop</a>, <a href="While.htm">While 循环</a>或 <a href="For.htm">For 循环</a>中仅含单行命令, 那么此命令可以不需要括在区块中. 不过, 这样做也许可以提高脚本的可读性和可维护性.</p>
<p>区块中可以为空(不含任何命令), 当您想注释区块中的内容而不移除区块时这很有用.</p>
<p id="otb"><strong>One True Brace(OTB, K&amp;R 风格):</strong> 在后面这些位置中可以使用 OTB 风格: <a href="IfExpression.htm">表达式型 if 语句</a>, <a href="Else.htm">else</a> 关键字, <a href="While.htm">while 循环</a>, <a href="For.htm">For 循环</a>, <a href="Loop.htm">普通循环</a>, <a href="../Functions.htm#define">函数定义</a>, <a href="Try.htm">Try</a>, <a href="Catch.htm">Catch</a> 和 <a href="Finally.htm">Finally</a>. 这种风格中区块的开括号与区块的控制语句在同一行, 而不是在其下一行. 例如:</p>
<pre>if (x &lt; y) {
    ...
} else {
    ...
}
While x &lt; y {
    ...
}
For k,v in obj {
    ...
}
Loop %RepeatCount% {
    ...
}
MyFunction(x, y) {
    ...
}
Try {
    ...
} Catch e {
    ...
} Finally {
    ....
}</pre>
<p>同样地, 命令或其他操作可以放在大括号的后面(但不包括 One True Brace 风格的开括号). 例如:</p>
<pre>if x = 1
{ MsgBox This line appears to the right of an opening brace. It executes whenever the IF-statement is true.
    MsgBox This is the next line.
} MsgBox This line appears to the right of a closing brace. It executes unconditionally.</pre>
<h2>相关</h2>
<p><a href="Functions.htm">函数</a>, <a href="While.htm">While 循环</a>, <a href="Loop.htm">Loop</a>, <a href="Else.htm">Else</a>, <a href="IfEqual.htm">If</a>, <a href="IfExpression.htm">If(Expression)</a></p>
<h2>示例</h2>
<pre class="NoIndent">if x = 1
{
    MsgBox, test1
    Sleep, 5
}
else
    MsgBox, test2</pre>

</body>
</html>